import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {
    private DatagramSocket socket=null;
    private String serverIP;
    private int serverPort;

    //客户端启动
    public UdpEchoClient(String serverIP,int serverPort) throws SocketException {
        //客户端不需要显示关联接口，但不代表没有端口，而是系统自动分配了空闲的端口
        socket=new DatagramSocket();
        this.serverIP=serverIP;
        this.serverPort=serverPort;
    }

    public void start() throws IOException {
        Scanner sc=new Scanner(System.in);
        while (true){
            //1.先从控制台读取一个字符串
            //先打印一个提示符，提示输入内容
            System.out.println("->");
            String request=sc.next();
            //2.把字符构造成UDP packet，并进行发送
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(serverIP),serverPort);
            socket.send(requestPacket);
            //3.客户端尝试读取服务器返回的响应
            DatagramPacket responsePacket =new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            //4.把响应数据转换成String显示出来
            String response=new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.printf("req:%s,resp:%s\n",request,response);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient udpEchoClient=new UdpEchoClient("127.0.01",9090);
        udpEchoClient.start();
    }
}
